/*
 * Copyright (c) 2016-2017 fulihui.com All right reserved
 * Project: information-core-170307
 * File name: DynamicNavbarConv.java
 */

package com.fulihui.information.biz.conv;

import com.fulihui.information.dal.dataobj.InfoDynamicNavbar;
import com.fulihui.information.dal.dataobj.InfoDynamicNavbarKey;
import com.fulihui.information.dto.DynamicNavbarDTO;
import com.fulihui.information.enums.EYesOrNo;
import com.fulihui.information.request.DynamicNavbarQuerySingleRequest;
import com.fulihui.information.request.DynamicNavbarSaveRequest;

import java.util.List;
import java.util.stream.Collectors;

import static com.fulihui.information.util.VersionFormat.atoi;
import static com.fulihui.information.util.VersionFormat.itoa;
import static java.util.Collections.emptyList;
import static org.springframework.util.CollectionUtils.isEmpty;

/**
 * @author Willard.Hu on 2017/3/3 0003.
 */
public class DynamicNavbarConv {

    public static InfoDynamicNavbar toDataObj(DynamicNavbarSaveRequest request) {
        if (request == null) {
            return null;
        }
        InfoDynamicNavbar record = new InfoDynamicNavbar();
        record.setOsType(request.getOsType());
        record.setVersion(atoi(request.getVersion()));
        record.setNavbarCode(request.getNavbarCode());
        record.setIcon(request.getIcon());
        record.setActivityIcon(request.getActivityIcon());
        record.setText(request.getText());
        record.setShowState(request.getShowState() ? EYesOrNo.YES.getCode() : EYesOrNo.NO.getCode());
        record.setOptionType(request.getOptionType());
        record.setContent(request.getContent());
        return record;
    }

    public static InfoDynamicNavbarKey tokey(DynamicNavbarQuerySingleRequest request) {
        if (request == null) {
            return null;
        }
        InfoDynamicNavbarKey key = new InfoDynamicNavbarKey();
        key.setOsType(request.getOsType());
        key.setVersion(atoi(request.getVersion()));
        key.setNavbarCode(request.getNavbarCode());
        return key;
    }

    public static InfoDynamicNavbarKey tokey(DynamicNavbarSaveRequest request) {
        if (request == null) {
            return null;
        }
        InfoDynamicNavbarKey key = new InfoDynamicNavbarKey();
        key.setOsType(request.getOsType());
        key.setVersion(atoi(request.getVersion()));
        key.setNavbarCode(request.getNavbarCode());
        return key;
    }

    public static DynamicNavbarDTO toDTO(InfoDynamicNavbar record) {
        if (record == null) {
            return null;
        }
        DynamicNavbarDTO dto = new DynamicNavbarDTO();
        dto.setOsType(record.getOsType());
        dto.setVersion(itoa(record.getVersion()));
        dto.setNavbarCode(record.getNavbarCode());
        dto.setIcon(record.getIcon());
        dto.setActivityIcon(record.getActivityIcon());
        dto.setText(record.getText());
        dto.setOptionType(record.getOptionType());
        dto.setContent(record.getContent());
        dto.setShowState(EYesOrNo.YES.getCode().equals(record.getShowState()));
        dto.gmtCreate(record.getGmtCreate());
        dto.gmtModified(record.getGmtModified());
        return dto;
    }

    public static List<DynamicNavbarDTO> toList(List<InfoDynamicNavbar> list) {
        if (isEmpty(list)) {
            return emptyList();
        }
        return list.stream().map(DynamicNavbarConv::toDTO).collect(Collectors.toList());
    }

}
